package com.dmall.order.service.impl;

import com.dmall.framework.result.DmallResult;
import com.dmall.jedis.JedisClient;
import com.dmall.order.info.OrderInfo;
import com.dmall.order.mapper.OrderItemMapper;
import com.dmall.order.mapper.OrderMapper;
import com.dmall.order.mapper.OrderShippingMapper;
import com.dmall.order.pojo.TbOrderItem;
import com.dmall.order.pojo.TbOrderShipping;
import com.dmall.order.service.OrderService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Chen 2017/11/19
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private JedisClient jedisClient;
    @Value("${ORDER_ID_GEN_KEY}")
    private String ORDER_ID_GEN_KEY;
    @Value("${ORDER_ID_START}")
    private String ORDER_ID_START;
    @Value("${ORDER_DETAIL_ID_GEN_KEY}")
    private String ORDER_DETAIL_ID_GEN_KEY;

    public DmallResult createOrder(OrderInfo orderInfo){
        OrderMapper orderMapper = sqlSessionTemplate.getMapper(OrderMapper.class);
        OrderItemMapper orderItemMapper = sqlSessionTemplate.getMapper(OrderItemMapper.class);
        OrderShippingMapper orderShippingMapper = sqlSessionTemplate.getMapper(OrderShippingMapper.class);
        //生成订单号。使用redis的incr生成。
        if (!jedisClient.exists(ORDER_ID_GEN_KEY)) {
            jedisClient.set(ORDER_ID_GEN_KEY, ORDER_ID_START);
        }
        String orderId = jedisClient.incr(ORDER_ID_GEN_KEY).toString();
        //补全orderInfo的属性
        orderInfo.setOrderId(orderId);
        //1、未付款，2、已付款，3、未发货，4、已发货，5、交易成功，6、交易关闭
        orderInfo.setStatus(1);
        orderInfo.setCreateTime(new Date());
        orderInfo.setUpdateTime(new Date());
        //插入订单表
        orderMapper.insert(orderInfo);
        //向订单明细表插入数据。
        List<TbOrderItem> orderItems = orderInfo.getOrderItems();
        for (TbOrderItem tbOrderItem : orderItems) {
            //生成明细id
            String odId = jedisClient.incr(ORDER_DETAIL_ID_GEN_KEY).toString();
            //补全pojo的属性
            tbOrderItem.setId(Long.parseLong(odId));
            tbOrderItem.setOrderId(orderId);
            //向明细表插入数据
            orderItemMapper.insert(tbOrderItem);
        }
        //向订单物流表插入数据
        TbOrderShipping orderShipping = orderInfo.getOrderShipping();
        orderShipping.setOrderId(orderId);
        orderShipping.setCreated(new Date());
        orderShipping.setUpdated(new Date());
        orderShippingMapper.insert(orderShipping);
        return DmallResult.ok(orderId);
    }
}
